﻿using Light.Assets.Scripts.EventManager;
using Refresh;
using UnityEngine;
using UnityEngine.UI;

namespace Light
{
    public class SkillDetail : MonoBehaviour, IRefresh
    {
        [SerializeField] private Button _updateButton;
        [SerializeField] private Image _buttonImage;
        private SkillItemSO _currentSkill;
        private Text _SkillCost;
        private Text _SkillDes;

        private Image _SkillIcon;
        private Text _SkillName;
        private Text _SkillPoints;
        private Text _SkillUpdate;
        private int curSkillPoints = 8;

        private void Awake()
        {
            _SkillIcon = transform.Find("SkillIcon").gameObject.GetComponent<Image>();
            _SkillName = transform.Find("SkillName").gameObject.GetComponent<Text>();
            _SkillCost = transform.Find("SkillCost").gameObject.GetComponent<Text>();
            _SkillDes = transform.Find("SkillDex").gameObject.GetComponent<Text>();
            _SkillUpdate = transform.Find("Update").gameObject.GetComponentInChildren<Text>();
            _SkillPoints = transform.Find("SkillPoints").gameObject.GetComponent<Text>();
        }

        private void OnEnable()
        {
            _updateButton.onClick.AddListener(UpdateSkill);
        }

        private void OnDisable()
        {
            _updateButton.onClick.RemoveAllListeners();
        }

        public void Refresh<T>(T evt) where T : RefreshData<T>, new()
        {
            var temp = evt as ItemDetailRefreshData;
            var data = temp.data;
            if (data is SkillItemSO)
            {
                var info = data as SkillItemSO;
                _currentSkill = info;
                RefreshSkillDetailData();
            }
        }

        public void UpdateSkill()
        {
            if (_currentSkill != null)
            {
                if (_currentSkill.pId.Count == 0)
                {
                    _currentSkill.isActive = true;
                    UnLockSkill();
                    RefreshSkillDetailData();
                }
                else
                {
                    var isUnlockable = true;
                    foreach (var id in _currentSkill.pId)
                    {
                        var skill = SkillsTables.Instance.GetSkillBySkillId(ItemType.Skill, id);
                        if (!skill.isActive)
                            isUnlockable = false;
                    }

                    if (isUnlockable)
                    {
                        _currentSkill.isActive = true;
                        UnLockSkill();
                        RefreshSkillDetailData();
                    }
                }
            }
        }

        public void UnLockSkill()
        {
            curSkillPoints -= _currentSkill.cost;
            _SkillPoints.text = "技能点：" + curSkillPoints + "/8";

            var evt = ChangeColorEvent.Get();
            evt.skill = _currentSkill;
            EventManager.SendEvent(evt);

            //增加解锁技能对应的属性
            var evt2 = UnolockSkillEvent.Get();
            evt2.skill = _currentSkill;
            EventManager.SendEvent(evt2);
        }

        public void RefreshSkillDetailData()
        {
            _SkillIcon.sprite = _currentSkill.image;
            _SkillName.text = "名称：" + _currentSkill.itemName;
            _SkillDes.text = "技能描述：" + _currentSkill.description;
            _SkillCost.text = "花费：" + _currentSkill.cost;
            if (_currentSkill.isActive)
            {
                _SkillUpdate.text = "已解锁";
                _updateButton.enabled = false;
                _buttonImage.color = Color.gray;
            }
            else
            {
                _SkillUpdate.text = "解锁";
                _updateButton.enabled = true;
                _buttonImage.color = Color.white;
            }
        }
    }
}